<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item v-if="!ruleForm.userId" label="用户名" prop="userName">
      <el-input v-model="ruleForm.userName"></el-input>
    </el-form-item>
    <!-- <el-form-item label="头像" prop="photo">
      <el-upload
        class="avatar-uploader"
        :action="baseURL + '/upload'"
        :headers="headers"
        :on-success="onSuccess"
        :limit="3"
        :file-list="fileList"
        :show-file-list="true">
        <img v-if="ruleForm.photo" :src="ruleForm.photo" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item> -->
    <el-form-item label="头像" prop="photo">
      <upload
        class="avatar-uploader"
        action="/upload"
        :headers="headers"
        :cicle="true"
        :on-success="onSuccess">
        <img v-if="ruleForm.photo" :src="ruleForm.photo" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </upload>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickName">
      <el-input v-model="ruleForm.nickName"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio v-model="ruleForm.sex" label="1">男</el-radio>
      <el-radio v-model="ruleForm.sex" label="2">女</el-radio>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">保存</el-button>
      <el-button @click="$router.back()">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import upload from '@/components/Upload'
export default {
  name: 'userEdit',
  components: {
    upload
  },
  data(){
    return {
      baseURL:process.env.VUE_APP_BASE_URL,//获取API请求地址
      ruleForm:{
        name:'',
        nickName:'',
        sex:1,
        id:'',
        photo:''
      },
      headers:{//获取token
        token:localStorage.getItem('token')
      },
      fileList:[],
      rules:{
        
      }
    }
  },
  methods:{
    submitForm(){
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$http({
            url:this.ruleForm.id ? '/user/update' : '/user/add',
            method:!this.ruleForm.id ? 'POST' : 'PUT',
            data:this.ruleForm
          }).then(res => {
            if(!res.errorMsg){
              this.$message({
                message: !this.ruleForm.id ? '添加用户成功' : '修改用户信息成功',
                type: 'success'
              });
              this.$router.back()
            }
          })
        } else {
          return false;
        }
      })
    },
    getDetail(id){
      this.$http({
        url:'/user/detail',
        params:{
          id
        }
      }).then(res => {
        if(!res.errorMsg) {
          this.ruleForm = res.data
        }
      })
    },
    onSuccess(res){
      //拼装图片路径赋值给表单
      this.ruleForm.photo = this.baseURL + '/file/' + res._id
    }
  },
  created(){
    let userId = this.$route.query.id
    if(userId) this.getDetail(userId)
  },
  mounted(){
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    border: 1px dashed #e4e4e4;
    background: #fff;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
